using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.Rendering;
using UnityEngine.UI;
using Cysharp.Threading.Tasks;
public class Eleven : MonoBehaviour
{
    public Button btn1;
    public Image img1;

    public Button btn2;
    public Image img2;

   string url= "https://www.baidu.com/img/PCfb_5bf082d29588c07f842ccde3f97243ea.png";
    void Start()
    {
        // StartCoroutine(Normal());
        btn1.onClick.AddListener(() => StartCoroutine(Normal()));
        btn2.onClick.AddListener(() => UniTaskImg().Forget());
    }

    
   public IEnumerator Normal()
    {
        using( var webRequest=UnityWebRequestTexture.GetTexture(url) )
        {
            yield return webRequest.SendWebRequest();
            if (webRequest.isHttpError || webRequest.isNetworkError) Debug.Log(webRequest.error);

            else
            {
                if(webRequest.isDone)
                {

                    var texture = ((DownloadHandlerTexture)webRequest.downloadHandler).texture;
                    Sprite sprite = Sprite.Create(texture, new Rect(Vector2.zero, new Vector2(texture.width, texture.height)), new Vector2(0.5f, 0.25f));
                    img1.sprite = sprite;
                }
            }
        }
    }

    
    private async UniTaskVoid UniTaskImg()
    {

        var webRequest = UnityWebRequestTexture.GetTexture(url);
        await webRequest.SendWebRequest();
        var texture = ((DownloadHandlerTexture)webRequest.downloadHandler).texture;
        Sprite sprite = Sprite.Create(texture, new Rect(Vector2.zero, new Vector2(texture.width, texture.height)), new Vector2(0.5f, 0.25f));
        img2.sprite = sprite;
    }
}
